package com.gaoxizhe.ccc.service;

import com.gaoxizhe.ccc.base.RestEntityResponse;
import com.gaoxizhe.ccc.constant.ReturnBaseConstant;
import com.gaoxizhe.ccc.dao.UserDao;
import com.gaoxizhe.ccc.model.login.User;
import com.gaoxizhe.ccc.model.login.VerificationCode;
import com.gaoxizhe.ccc.util.MailUtil;
import com.gaoxizhe.ccc.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 用户服务层
 *
 * @author 廖某某
 * @date 2019/02/17
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    UserDao userDao;

    private Integer respEntity = 0;

    private User user = new User();

    private VerificationCode verificationCode = new VerificationCode();

    /**
     * 发送验证码
     *
     * @param email
     * @return
     */
    public RestEntityResponse<User> sendCode(String email) {
        RestEntityResponse<User> userRestEntityResponse = new RestEntityResponse<>();
        userRestEntityResponse.setCode(0);
        userRestEntityResponse.setMsg("success");
        try {
            //产生随机的验证码
            String code = RandomStringUtils.randomNumeric(6);
            User user = new User();
            user = userDao.isExistUser(email);
            if (user == null) {
                System.out.println("邮箱：" + email + "--验证码为:" + code);
                //发送邮件开始 发送验证码
                MailUtil.sendeMail("注册验证码", String.valueOf(code), Collections.singletonList(email), null, null, null);
                //修改数据库中的验证码
                verificationCode = userDao.checkCode(email);
                if (verificationCode != null) {
                    userDao.changeCode(email, code);
                } else {
                    //保存验证码信息到数据库
                    userDao.saveCode(email, code);
                }
            } else {
                userRestEntityResponse.setCode(ReturnBaseConstant.USER_NONENTITY);
                userRestEntityResponse.setMsg("用户不存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return userRestEntityResponse;
    }

    /**
     * 注册信息提交
     *
     * @param email
     * @param nickName
     * @param password
     * @param registerCode
     * @return
     */
    public RestEntityResponse<User> RegisterInfo(String email, String nickName, String password, String registerCode) {
        RestEntityResponse<User> userRestEntityResponse = new RestEntityResponse<>();
        userRestEntityResponse.setCode(0);
        userRestEntityResponse.setMsg("success");

        verificationCode = userDao.checkCode(email);
        if (verificationCode != null) {
            if (registerCode.equals(verificationCode.getCode())) {
                //时间校验--暂略
                User user = new User();
                user.setEmail(email);
                user.setPassword(password);
                user.setNickname(nickName);
                userDao.addUser(user);
                //删除验证码信息
                userDao.deleteCode(email);
            } else {
                userRestEntityResponse.setCode(ReturnBaseConstant.CODE_ERROR);
                userRestEntityResponse.setMsg("验证码错误");
            }
        } else {
            userRestEntityResponse.setCode(ReturnBaseConstant.USER_NONENTITY);
            userRestEntityResponse.setMsg("用户不存在");
        }
        return userRestEntityResponse;
    }

    /**
     * 登录验证
     *
     * @param email
     * @param password
     * @return
     */
    public  RestEntityResponse<String> Login(String email, String password) {
        RestEntityResponse<String> userRestEntityResponse = new RestEntityResponse<>();
        userRestEntityResponse.setCode(0);
        userRestEntityResponse.setMsg("success");
        user = userDao.login(email, password);
        String token = "";
        if (user != null) {
            token = TokenUtil.createJwtToken(email);
            userRestEntityResponse.setData(token);
        } else {
            userRestEntityResponse.setCode(ReturnBaseConstant.USER_NONENTITY);
            userRestEntityResponse.setMsg("用户不存在或者用户名密码错误");
            log.info("用户不存在或者用户名密码错误");
        }
        return userRestEntityResponse;
    }

    /**
     * 根据旧密码更改密码
     *
     * @param usedPassword
     * @return
     */
    public Integer ChangePassword(String email, String usedPassword, String newPassword) {
        user = userDao.login(email, usedPassword);
        if (user == null) {
            respEntity = -1;
        } else {
            userDao.ChangePassword(email, newPassword);
        }
        return respEntity;
    }
}